/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package dBasea;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DatuBasea {

	private static DatuBasea instance = new DatuBasea();

	public static DatuBasea getInstance() {
		if (instance == null) {
			instance = new DatuBasea();
		}
		return instance;
	}

	private Connection konexioa = null;

	private DatuBasea() {
		try {
			this.konektatu();
		} catch (Exception e) {
		}
	}

	private void konektatu() throws ClassNotFoundException, SQLException {
		try {

			Properties props = new Properties();
			FileInputStream file = new FileInputStream(
					"./konfig/datubasea.properties");
			props.load(file);
			file.close();

			Class.forName("com.mysql.jdbc.Driver");
			String url = props.getProperty("jdbc.url");
			String erab = props.getProperty("jdbc.username");
			String pass = props.getProperty("jdbc.password");
			konexioa = DriverManager.getConnection(url, erab, pass);

			System.out.println("Database connection established");
		} catch (Exception e) {
			System.err.println("Cannot connect to database server");
		}
	}

	private ResultSet query(Statement s, String query) {
		ResultSet rs = null;

		try {
			rs = s.executeQuery(query);
			try {
				this.konektatu();
			} catch (Exception e) {
			}
			/*
			 * for (;;) { while (rs.next()) { } if (s.getMoreResults()) { rs =
			 * s.getResultSet(); s.getMoreResults(Statement.CLOSE_ALL_RESULTS);
			 * s.close(); } else { break; } }
			 */
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return rs;
	}

	public ResultSet executeSQL(String query) {
		int count = 0;
		Statement s = null;
		ResultSet rs = null;

		try {
			s = konexioa.createStatement();
			if (query.toLowerCase().indexOf("select") == 0) {
				rs = this.query(s, query);

			} else {
				count = s.executeUpdate(query);
				System.out.println(count + " rows affected");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return rs;
	}

	public void itzali() throws SQLException {
		if (konexioa != null)
			try {
				konexioa.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		System.out.println("Database connection terminated");
	}

}